import re
import json
import requests
import ast

url = 'https://vip.titan007.com/count/goalData.aspx?p=1&sp=1&t=1&time=2025/9/14%2016:30:00&hw=0.98&gw=0.83&g=0&cid=8&r=1&t2=1&scid=1122&flesh=0.9013472854271025'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
}

response = requests.get(url, headers=headers)

# 步骤1：使用正则表达式提取变量名和对应的值
pattern = r'var\s+(\w+)\s*=\s*([^;]+);'
matches = re.findall(pattern, response.text)
# print(matches)

# 步骤2：改进的解析方法
variables = {}
for var_name, var_value in matches:
    # 清理值
    cleaned_value = var_value.strip().rstrip(';')
    
    try:
        # 首先尝试用json解析
        variables[var_name] = json.loads(cleaned_value)
    except json.JSONDecodeError:
        try:
            # 如果json失败，尝试用ast.literal_eval解析（适用于数组、字典等）
            variables[var_name] = ast.literal_eval(cleaned_value)
        except (ValueError, SyntaxError):
             # 对于无法解析的JavaScript数组，尝试手动处理
            if cleaned_value.startswith('[') and cleaned_value.endswith(']'):
                try:
                    # 使用eval作为最后手段（注意：仅在可信数据源使用）
                    variables[var_name] = eval(cleaned_value)
                except:
                    # 如果都失败，保存为原始字符串
                    variables[var_name] = cleaned_value
            else:
                # 保存为原始字符串
                variables[var_name] = cleaned_value


# 结果
goalPageInfo = variables['goalPageInfo']
goalPageData = variables['goalPageData']
a = list(goalPageData)

# print(type(goalPageInfo))
# print(type(a))

print(variables['goalPageInfo'])
print(variables['goalPageData'])